package com.winshare.saleAssistant.domain.dto;

import com.alibaba.fastjson.JSON;

import java.io.Serializable;

/**
 * 返回结果集
 * @author whz
 * @param <T>
 */
public class RDto<T> implements Serializable {

    private boolean success;

    private String statusCode;

    private String msg;

    private T data;

    public static final String SUCCESS = "200";

    public static final String ERROR = "500";


    private RDto() {

    }

    private RDto(boolean success, String statusCode, String msg, T data) {
        this.success = success;
        this.statusCode = statusCode;
        this.msg = msg;
        this.data = data;
    }

    public static <T> RDto<T> success() {
        return new RDto<>(true,SUCCESS,null,null);
    }
    public static <T> RDto<T> success(T data) {
        return new RDto<>(true,SUCCESS,null,data);
    }

    public static <T> RDto<T> success(String msg) {
        return new RDto<>(true,SUCCESS,msg,null);
    }

    public static <T> RDto<T> success(T data, String msg) {
        return new RDto<>(true,SUCCESS,msg,data);
    }
    public static <T> RDto<T> fail() {
        return new RDto<>(false,ERROR,null,null);
    }
    public static <T> RDto<T> fail(T data) {
        return new RDto<>(false,ERROR,null,data);
    }
    public static <T> RDto<T> fail(String msg) {
        return new RDto<>(false,ERROR,msg,null);
    }

    public static <T> RDto<T> fail(String msg, String code) {
        return new RDto<>(false,code,msg,null);
    }

    public static <T> RDto<T> fail(T data, String msg) {
        return new RDto<>(false,ERROR,msg,data);
    }

    public static <T> RDto<T> fail(T data, String msg, String code) {
        return new RDto<>(false,code,msg,data);
    }

    public RDto(Throwable e) {
        this.msg = e.getMessage();
        this.setSuccess(false);
        this.statusCode = "1";
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
